package com.lpit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpit.constant.Constant;
import com.lpit.dto.BarChartDto;
import com.lpit.dto.UserDto;
import com.lpit.exception.AdminException;
import com.lpit.mapper.MessageMapper;
import com.lpit.pojo.Message;
import com.lpit.pojo.User;
import com.lpit.service.UserService;
import com.lpit.mapper.UserMapper;
import com.lpit.utils.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2023-06-08 11:06:01
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查看所有用户列表以及实名状态
     * @param page 当前页
     * @param pageSize 一页多少条
     * @param userDto 条件参数
     * @authot 杨文
     * @return
     */
    @Override
    public IPage<UserDto> lookUsers(Integer page, Integer pageSize, UserDto userDto) {
        /*模糊条件查询*/
        LambdaQueryWrapper<UserDto> wrapper = new LambdaQueryWrapper<UserDto>();
        wrapper.eq(Tool.isNotEmpty(userDto.getStatus()),UserDto::getStatus, userDto.getStatus());
        wrapper.like(Tool.isNotEmpty(userDto.getNumber()),UserDto::getNumber, userDto.getNumber());
        /*设置分页*/
        Page<UserDto> page1 = new Page<UserDto>(page, pageSize);

        return userMapper.lookUsers(wrapper,page1);

    }

    /*根据id批量修改用户状态*/
    /*
     * 杨文
     * */
    @Override
    public boolean updateUsersStatus(List<Integer> integers, String status) {
        System.err.println(status);
        /*判断请求是否是冻结用户的，是冻结用户就将状态修改为冻结，否则就是正常*/
        status = status.equals(Constant.HTTP_FREEZE_URI) ? Constant.ACCOUNT_STATUS_FREEZE : Constant.ACCOUNT_STATUS_THAW;
        /*创建集合容器*/
        List<User> statusList = new ArrayList<>();
        /*循环创建修改信息对象，将对象传入list*/
        for (Integer integer : integers) {
            User user = new User();
            user.setId(integer);
            user.setStatus(status);
            statusList.add(user);
        }
        /*调用mybatisPlus批量修改方法并返回结果*/
        return this.updateBatchById(statusList);
    }
    /*
     * 修改用户密码功能
     * 三个数据：账号，原密码，和新密码
     * 数据库内的密码是m5加密过的
     * */
    @Override
    public String reviseUserAdmin(String name, String password, String revisePassword){
        /*判断用户账号是否存在*/
        String adminUsername = userMapper.selectUserSelective(name);
        if(adminUsername==null){
            return "用户不存在";
        }
        /*将原密码进行m5加密从数据库内进行判断*/
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        /*接收到数据库反馈出来的有无数据，来判断是否正确 */
        name = userMapper.selectUserReviseAdmin(name,password);
        System.out.println("password"+password);
        if(password == null){
            return "原密码错误";
        }
        /*通过后将新密码进行m5加密*/
        revisePassword = DigestUtils.md5DigestAsHex(revisePassword.getBytes());
        /*将加密后的reviseAdmin值添加到数据库内*/
        userMapper.updateUserReviseAdmin(revisePassword,name);
        return "修改成功";
    }

    /**
     * @author yao chen
     * @return List
     */
    @Override
    public String dailyActiveUsers() {
        String loginTime = userMapper.selectUserLoginTime();
        if(null == loginTime ){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return loginTime;
    }

    /**
     * @author yao chen
     * @return List<BArChartDto>
     *     当日新增用户
     */
    @Override
    public String newUsers() {
        String barChartDtoList = userMapper.selectNewUsers();
        if(null == barChartDtoList ){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return barChartDtoList;
    }
    /**
     * 查询日均活跃用户
     * @author yao chen
     * @retunt List
     */
    @Override
    public List<BarChartDto> averageDailyActiveUsers() {
        List<BarChartDto> barChartDtoList = userMapper.selectAverageDailyActiveUsers();
        if(null == barChartDtoList || 0 == barChartDtoList.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return barChartDtoList;
    }
    /**
     * 查询日均新增用户
     * @author yao chen
     * @retunt List
     */
    @Override
    public List<BarChartDto> averageNewUserPerDay() {
        List<BarChartDto> barChartDtoList = userMapper.selectAverageNewUserPerDay();
        System.out.println(barChartDtoList);
        if(null == barChartDtoList || 0 == barChartDtoList.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return barChartDtoList;
    }
    /**
     * 查询某段时间的活跃用户
     * Query the active users in a specified period of time
     * @author yao chen
     */
    @Override
    public List<BarChartDto> queryTheActiveUsersSpecifiedPeriodOfTime(String kDate, String jDate) {
        List<BarChartDto> queryTheActiveUsersSpecifiedPeriodOfTime = userMapper.queryTheActiveUsersSpecifiedPeriodOfTime(kDate, jDate);
        if(null == queryTheActiveUsersSpecifiedPeriodOfTime || 0 == queryTheActiveUsersSpecifiedPeriodOfTime.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }
        return queryTheActiveUsersSpecifiedPeriodOfTime;
    }

    @Override
    public List<BarChartDto> userAddedWithinSpecifiedPeriod(String kDate, String jDate) {
        List<BarChartDto> barChartDtos = userMapper.userAddedWithinSpecifiedPeriod(kDate, jDate);
        if(null == barChartDtos || 0 == barChartDtos.size()){
            throw new AdminException(Constant.NETWORK_ERROR);
        }

        return barChartDtos;
    }

}




